package com.zlzlib.libui.util;

import android.content.Context;

/**
 * Created by zlz on 2018/6/7.
 */
public class PxUtil {

    public static final int CHINESE = 1;
    public static final int NUMBER_OR_CHARACTER = 2;


    public static float getScale(Context context) {
        return context.getResources().getDisplayMetrics().density;
    }

    public static float getScaleDensity(Context context) {
        return context.getResources().getDisplayMetrics().scaledDensity;
    }

    /**
     * @param dipValue dp
     * @return dp转成px
     */
    public static int dip2px(Context context, float dipValue) {
        return (int) (dipValue * getScale(context) + 0.5f);
    }

    /**
     * @param pxValue px
     * @return px转成dp
     */
    public static int px2dip(Context context, float pxValue) {
        return (int) (pxValue / getScale(context) + 0.5f);
    }

    /**
     * @param spValue sp
     * @param type    文字类型
     * @return sp转成px
     */
    public static float sp2px(Context context, float spValue, int type) {
        if (type == NUMBER_OR_CHARACTER) {
            return spValue * getScaleDensity(context) / 1.8f;
        }
        return spValue * getScaleDensity(context);
    }

    /**
     * @param pxValue px
     * @param type    文字类型
     * @return px转成sp
     */
    public static float px2sp(Context context, float pxValue, int type) {
        if (type == NUMBER_OR_CHARACTER) {
            return pxValue / getScaleDensity(context) * 1.8f;
        }
        return pxValue / getScaleDensity(context);
    }

}
